package com.li.controller;

import com.li.pojo.po.Menu;
import com.li.pojo.po.Result;
import com.li.pojo.po.Role;
import com.li.pojo.po.RoleIdMenusResult;
import com.li.pojo.vo.MenuVO;
import com.li.service.MenuService;
import com.li.service.RoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/system/menu")
@Slf4j
public class SysMenuController {
    @Autowired
    private MenuService menuService;

    @Autowired
    private RoleService roleService;

    /**
     * 菜单列表
     * @param menuName
     * @param status
     * @return
     */
    @GetMapping("/list")
    public Result getMenuList(String menuName,String status){
        List<Menu> list = menuService.getMenuList(menuName,status);
        return Result.success(list);
    }

    /**
     * 菜单树
     * @param menu
     * @return
     */
    @GetMapping("/treeselect")
    public Result treeSelectMenu(Menu menu){
        List<MenuVO> menuVOList = menuService.treeSelectMenu();
        return Result.success(menuVOList);
    }

    /**
     * 添加菜单
     * @param menu
     * @return
     */
    @PostMapping()
    public Result addMenu(@RequestBody Menu menu){
        menuService.addMenu(menu);
        return null;
    }

    /**
     * 根据id回显菜单
     * @param menuId
     * @return
     */
    @GetMapping("/{menuId}")
    public Result getMenuById(@PathVariable Long menuId){
        return Result.success( menuService.getById(menuId));
    }

    /**
     * 修改菜单
     * @param menu
     * @return
     */
    @PutMapping
    public Result updateMenu(@RequestBody Menu menu){
        menuService.updateMenu(menu);
        return Result.success();
    }

    /**
     * 根据id删除菜单
     * @param menuId
     * @return
     */
    @DeleteMapping("/{menuId}")
    public Result deleteMenuId(@PathVariable Long menuId){
        menuService.deleteMenuId(menuId);
        return Result.success();
    }


    @GetMapping("/roleMenuTreeselect/{roleId}")
    public RoleIdMenusResult roleMenuTreeselect(@PathVariable Integer roleId){
        Role role = roleService.getById(roleId);
        String menuIds = role.getMenuIds();
        System.err.println(menuIds);
        List<String> list = new ArrayList<>();
        if (menuIds != null) {
            String[] split = menuIds.split(",");
            list.addAll(Arrays.asList(split));
        }
        List<MenuVO> menuVOList = menuService.treeSelectMenu();
        return new RoleIdMenusResult(200,list,menuVOList,"查询成功");
    }
}
